package com.xoa.controller.sys;

import com.xoa.dao.sys.SysInterfaceMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.sys.InterfaceModel;
import com.xoa.model.users.Users;
import com.xoa.service.sys.SystemInfoService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.LoginState;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.MobileCheck;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @创建作者: 韩成冰
 * @创建日期: 2017/5/31 18:01
 * @类介绍: 系统参数类
 * @构造参数: 无
 **/
@Controller
@RequestMapping("sys")
public class SystemInfoController {

    @Resource
    private SystemInfoService systemInfoService;
    @Resource
    private UsersMapper usersMapper;

    @Resource
    private SysInterfaceMapper sysInterfaceMapper;

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/5/31 19:54
     * @函数介绍: 获取系统信息
     * @参数说明: @param request
     * @return: json
     **/
    @ResponseBody
    @RequestMapping("/getSysMessage")
    public ToJson<Object> getSysMessage(HttpSession session ,HttpServletRequest request) {


        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        Object locale = request.getSession().getAttribute("LOCALE_SESSION_ATTRIBUTE_NAME");
        ToJson<Object> toJson = new ToJson<Object>(0, "");
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/");
            Map<String, String> map = systemInfoService.getSystemInfo(realPath, locale,request);
            //String contextPath = request.getContextPath();
            String serverPath = System.getProperty("user.dir");


            if(map!=null&&map.size()>0){
                Iterator i = map.entrySet().iterator();
                while (i.hasNext()) {
                    Map.Entry entry = (Map.Entry) i.next();
                    if(entry.getKey().equals("module")){
                        session.setAttribute("module",entry.getValue()) ;
                        break;
                    }

                }
            }else{
                map = new HashMap<>();
                map.put("authorizationCode",systemInfoService.getMachineCode16());
                SessionUtils.putSession(request.getSession(), "checkRegister", "1", redisSessionCookie);
            }

            //服务器路径倒退连接就是按照路径
            try {
                map.put("installPath", realPath + "0");
                if (serverPath != null) {
                    String[] pathArr = realPath.split("\\\\");
                    StringBuffer sb = new StringBuffer();
                    //windows
                    if("\\".equals(File.separator)){
//                        for(int i=0;i<pathArr.length-4;i++){
//                            sb.append(pathArr[i]+"/");
//                        }
                        String windowIns=realPath.split("webapps")[0];
                        map.put("installPath", windowIns);
                    }
                    //linux
                    if("/".equals(File.separator)){
                        //取到的是usr/local/tomcat/webapps/，应该加上xoa/ 所以，应该是le+3
//                        int le = realPath.lastIndexOf("xoa");
//                        String substring = realPath.substring(0,le+3);
//                        String[] split2 = substring.split("/");
//                        StringBuffer sb1 = new StringBuffer();
//                        for (int i = 0; i < split2.length; i++) {
//                            sb1.append(split2[i]).append("/");
//                        }
                        //map.put("installPath", sb1.toString());
                    }
                }

            }catch (Exception e){
                //出现问题
                e.printStackTrace();
                map.put("installPath", "--");
            }



            //获取服务器绝对路径
            //URL resource = Thread.currentThread().getContextClassLoader().getResource("");
            map.put("serverPath", serverPath);

            toJson.setMsg("OK");
            toJson.setFlag(0);
            toJson.setObject(map);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg("err");
        }

        return toJson;
    }


    /**
     * @创建作者: 张丽军
     * @创建日期: 2019/4/18 16:48
     * @函数介绍: 写入信息生成授权文件
     * @参数说明:
     * @return: json
     **/
    @RequestMapping(value = "/importLec")
    @ResponseBody
    public ToJson<Object> importLec(String fileString, HttpServletRequest request) throws Exception {
        ToJson<Object> toJson = new ToJson<Object>(0, "");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        String realPath = request.getSession().getServletContext().getRealPath("/");
        try {
            //判断字符串为不为空
            if (fileString != null && !fileString.equals("")) {
                //fileString =  java.net.URLDecoder.decode(fileString,"UTF-8");
                //新文件
                File newFile = new File(realPath.concat("/authfiles/xoa2019.dat"));

                //将字符串写入新文件
                try (FileWriter writer = new FileWriter(newFile);){
                    String str = fileString.substring(0,7);
                    if(str.equals("gaosubo")==true){

                        writer.write(fileString);
                    }else{
                        String fileString0 = "gaosuboisnumber1-79,-79,-66,-87,-68,-81,-51,-59,-41,-36,-71,-85,-53,-66,@123456789qwertyuiopasdfghjklzxcvbnm,.-@39amKearhtfuLiwH0EN/Wbey4dpcyuO5+wqjucCioXR2ZyxsQOkxm8Aa9X1P2bO2jYbPhcNdvcSIpgSgIKzDHSedVLxeiqVF/";

                        writer.write(fileString0);
                    }
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            toJson.setFlag(0);
            toJson.setMsg("文件写入成功");

        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }





    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/28 15:48
     * @函数介绍: 上传授权文件
     * @参数说明:
     * @return: json
     **/
    @RequestMapping(value = "/uploadLec")
    @ResponseBody
    public ToJson<Object> editItemSubmit(MultipartFile lecFile, HttpServletRequest request) throws Exception {
        ToJson<Object> toJson = new ToJson<Object>(0, "");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);

        boolean isMatch = Pattern.matches(".*dat.*", lecFile.getOriginalFilename());
             if(!isMatch){
                 toJson.setFlag(1);
                 toJson.setMsg("err");
                 return toJson;
             }

        String realPath = request.getSession().getServletContext().getRealPath("/");

        try {
            //实现上传图片
            if (lecFile != null && lecFile.getOriginalFilename() != null
                    && !lecFile.getOriginalFilename().equals("")) {

                //获取图片原始名称，目标要从原始名称中获取文件的扩展名
                String originalFilename = lecFile.getOriginalFilename();
                //新文件名称
                String fileName_new = "xoa2020";
                //新文件
                File newFile = new File(realPath.concat("/authfiles/xoa2020.dat"));
                //将内存中的文件内容写入磁盘上
                lecFile.transferTo(newFile);
            }
            toJson.setFlag(0);
            toJson.setMsg("授权成功");

        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg("err");

        }

        return toJson;
    }
        @RequestMapping(value = "/getAPPMessage")
    @ResponseBody
    public ToJson<Object> getAPPMessage(HttpServletRequest request){
        Map<String, String> map=systemInfoService.getAPPMessage();


        ToJson<Object> toJson = new ToJson<Object>(0, "");

        toJson.setMsg("OK");
        toJson.setFlag(0);
        toJson.setObject(map);

    return toJson;
    }

    @ResponseBody
    @RequestMapping("/isLoginRole")
    public ToJson<Object> isLoginRole(HttpServletRequest request, HttpServletResponse response){
        ToJson<Object> json = new ToJson<Object>(0, "");

        Object locale = request.getSession().getAttribute("LOCALE_SESSION_ATTRIBUTE_NAME");
        String realPath = request.getSession().getServletContext().getRealPath("/");
        try{
            Map<String, String> m = systemInfoService.getSystemInfo(realPath, locale, request);
            int oaUserLimit=0;
            if("不限制".equals(m.get("oaUserLimit"))){

            }else{
                //授权人数
                oaUserLimit = Integer.parseInt(m.get("oaUserLimit"));
            }
            Map<String,String> endLecDate= systemInfoService.getEndLecDate(request);
            String  useEndDate=  endLecDate.get("useEndDate");
            String  endLecDateStr=   endLecDate.get("endLecDateStr");
            String isAuthStr=endLecDate.get("isAuthStr");
            String usercount = endLecDate.get("usercount");
            L.s(useEndDate,"0=||===========>",endLecDateStr);
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            String msg = "";
            if(user.getUserId()!=null){
                json.setTurn(true);
            }else{
                msg= "当前用户未登录";
                json.setMsg(msg);
                InterfaceModel template = sysInterfaceMapper.getTemplate();
                json.setObject(template.getTemplate());
                json.setTurn(false);
            }
            //允许登录用户数
            int canLoginUser = usersMapper.getLoginUserCount();
            if(oaUserLimit!=0){
                if (canLoginUser > oaUserLimit) {
                    json.setCode(LoginState.LOGINTIME);
                    msg="允许登录OA用户达到上限，请联系管理员！";
                    json.setMsg(msg);
                    json.setTurn(true);
                    return json;
                }
            }
            if(checkTimeOut(useEndDate,endLecDateStr,isAuthStr,usercount)){//验证到期{
                if(MobileCheck.isMobileDevice(request.getHeader("user-agent"))){
                }else{
                    SessionUtils.putSession(request.getSession(), "checkRegister", "1", redisSessionCookie);
                    msg="到期";
                    json.setMsg(msg);
                    json.setTurn(true);
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return  json;
    }

    private boolean checkTimeOut(String useEndDate, String endLecDateStr,String isAuthStr,String usercount) {
        try{
            //判断是否注册
            if(isAuthStr.equals("true")){
                Date now =new Date();
                if(StringUtils.checkNull(endLecDateStr)){
                    return true;
                }else{
                    if("2040-01-01".equals(endLecDateStr)){
                        return false;
                    }else {
                        Date timeOut= DateFormat.DateToStr(endLecDateStr);
                        if(timeOut.getTime()<now.getTime()){
                            return true;
                        }
                    }
                }
                if(StringUtils.checkNull(useEndDate)){
                    return true;
                }else if(useEndDate.equals("0000-00-00")){
                    return false;
                }else{
                    Date timeOut= DateFormat.DateToStr(useEndDate);
                    if(timeOut.getTime()<now.getTime()){
                        return true;
                    }
                }
            }else{
                //先去判断用户数是否是30
                if(usercount.equals("30")){
                    return  false;
                }
                Date now =new Date();
                //如果用户数大于50人的话，判断试用是否到期
                if(StringUtils.checkNull(useEndDate)){
                    return true;
                }else if(useEndDate.equals("0000-00-00")){
                    return false;
                }else{
                    Date timeOut= DateFormat.DateToStr(useEndDate);
                    if(timeOut.getTime()<now.getTime()){
                        return true;
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            return true;
        }
        return false;
    }



}